package com.spring.cloud.study.user.controller;

import com.spring.cloud.study.common.entity.SysUser;
import com.spring.cloud.study.user.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


/**
 * Created with IntelliJ IDEA.
 * User: 齐丹枫
 * Date: 2018/2/27
 * Time: 17:33
 * To change this template use File | Settings | File Templates.
 * Description:
 */
@RestController
@RequestMapping("/api/sys/sysUser")
public class SysUserController {


    @Autowired
    private SysUserService sysUserService;

    @RequestMapping("/findUserByUsername")
    public SysUser findByUserName(@RequestParam("username") String username){
       // String userName = PrincipalUtils.getUserName();
        SysUser sysUser = sysUserService.findUserByUsername(username);
        return sysUser;
    }


/*

    @Autowired
    private SysUserService sysUserService;

    @RequestMapping("/save")
    public R save(@RequestBody SysUser sysUser){

        sysUserService.save(sysUser);

        return R.ok("保存成功");
    }

    @RequestMapping("/update")
    public R update(@RequestBody SysUser sysUser){

        sysUserService.update(sysUser);

        return R.ok("修改成功");
    }

    @RequestMapping("/getUserDetail")
    public R findByUserName(){
        String userName = PrincipalUtils.getUserName();
        SysUser sysUser = sysUserService.getUserDetail(userName);
        return R.ok().put("data",sysUser);
    }


    @RequestMapping("/saveBatch")
    @PreAuthorize("hasAuthority('sys:user:save')")
    public void saveBatch(@RequestParam(required = false) List<SysUser> sysUsers){
        sysUsers = new ArrayList<>();

        sysUserService.saveBatch(sysUsers);

    }

    @RequestMapping("/findUser")
    public R findUser(@RequestParam Map<String,Object> params){

        Page<SysUser> page = sysUserService.findPage(params);
        List<SysUser> content = page.getContent();

        PageUtils pageUtils = new PageUtils(content,page.getTotalElements(),page.getNumberOfElements(),Integer.parseInt(params.get("page").toString()));

        return R.ok().put("data",pageUtils);
    }

    @RequestMapping("/deleteUserById")
    public R deleteById(@RequestParam(value = "id") int id){
        try {
            sysUserService.delete(id);
        }catch (Exception e){
            return R.error();
        }
       return R.ok();
    }

    @RequestMapping("/findUserById")
    public R findUserById(@RequestParam(value = "id") int id){
        try {
            SysUser sysUser = sysUserService.findById(id);
            return R.ok().put("data",sysUser);
        }catch (Exception e){
            return R.error();
        }

    }

*/


}
